/*
 *说明：选择命令策略脚本
 *
 *@author liuxun 2012-03-19
*/

var opeForm;
var grid;
var cmd_policyGrid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		title: '命令策略列表(双击选定记录行)',
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'命令策略代码',field:'cmd_policy_code',align:'center',width:80},

            {title:'命令策略名称',field:'cmd_policy_name',align:'center',width:80},
            
            {title:'命令策略级别',field:'cmd_policy_level_decode',align:'center',width:80}
        ]],
		pagination:true,
		singleSelect: true,
		onDblClickRow: function(rowIndex, rowData) {
	    	window.returnValue = rowData;
	    	window.close();
	    },
		onClickRow: function(rowIndex, rowData) {
			var cmd_policy_id = rowData.cmd_policy_id;
			queryCmdPolicyOperation(cmd_policy_id);
	    }
	});
	
	cmd_policyGrid=$('#cmd_policylistTable').datagrid({
		title: '命令列表',
		columns:[[

            {title:'命令代码',field:'cmd_code',align:'center',width:80},

            {title:'命令名称',field:'cmd_value',align:'center',width:150}
        ]],
		pagination:false,
		singleSelect: false
	});

	queryOperation();
	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#okBtn').click(doOk);
	$('#cancelBtn').click(closeWin);
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'D_CMD_POLICY!queryList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
	
	queryCmdPolicyOperation(null);
}

function queryCmdPolicyOperation(cmd_policy_id){
	var params = opeForm.serializeObject();
	cmd_policyGrid.datagrid("options").pageNumber = 1;
	cmd_policyGrid.datagrid({
		url: 'D_CMD_INFO!queryList.do?model.cmd_policy_id=' + cmd_policy_id
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//保存按钮操作
function doOk() {

	var validateResult = opeForm.form('validate');

	if (!validateResult){
		showInfoWin('请正确填写信息!');
		return;
	}
	
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	} 
	
	if (rows.length > 1) {
		showInfoWin('只能选择一种策略!');
		return;
	}
	
	var cmd_policy_id = rows[0].cmd_policy_id;

	var params = $ .param({'model.policy_id' : cmd_policy_id}, true);

	var options = { 
			url: 'D_ACL!doUpdate.do?' + params,
			type: 'post',
			success: function(data){
				getOpenerWin().afterOK();
				//showInfoWin('操作成功!');
				 window.close();
			}
	};

	opeForm.ajaxSubmit(options);
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}